from threading import Thread

from DIDsocket import DIDSocket


# 使用示例
def server_example(own_did, port):
    """服务器使用示例"""
    did_socket = DIDSocket(own_did)
    did_socket.listen(port)

    while True:
        # 接受新连接
        connection, addr = did_socket.accept()
        if not connection:
            continue

        # 启动新线程处理这个连接
        def handle_connection(conn):
            while True:
                data = did_socket.recv(conn)
                if not data:
                    break
                print(f"收到    {data}")
                # 可以在这里添加回复逻辑
                did_socket.send(conn, "已收到")

            did_socket.close_connection(conn)

        Thread(target=handle_connection, args=(connection,), daemon=True).start()



did="did:p2p_did_chat:g05JbAygra"
port=456
server_example(did,port)

